//
//  LoginViewModel.swift
//  Exchange
//
//  Created by rui on 2018/6/4.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

class LoginViewModel: BaseViewModel {
    private var account: String = ""
    private var password: String = ""
    
    private var googleCode: String = ""
    
    private var googleToken: String = ""
    private var loginName:String = ""
    
    private lazy var loginApi: LoginRequest = {
        let login = LoginRequest()
        login.dataReformer = self
        login.paramSource = self
        login.delegate = self
        return login
    }()
    
    private lazy var googleApi: GoogleCheckRequest = {
        let api = GoogleCheckRequest()
        api.dataReformer = self
        api.paramSource = self
        api.delegate = self
        return api
    }()

    func login(account: String,password: String) {
        self.account = account
        self.password = password
        self.loginApi.loadData()
    }
}

extension LoginViewModel: RequestCallBack,RequestDataReformer,RequestParamSource {
    
    func reform(_ request: BaseRequest, _ data: [String : Any]?) {
        dPrint(data)
        if let _ = request as? LoginRequest {
            if let userInfo = data?["data"] as? [String:Any] {
                if let googleToken = userInfo["googleToken"] as? String,googleToken.count > 0 {
                    self.googleToken = googleToken
                    if userInfo["loginName"] as? String != nil {
                        self.loginName = userInfo["loginName"] as! String
                    }
                    ///验证谷歌
                    self.popGoogleInputView()
                }else {
                    let appInfo = AppInfo.default
                    AppInfo.default.account = self.account
                    appInfo.install(data: userInfo)
                    self.response?(ResponseState.mine_login,true, nil)
                }
            }
        }else  if let _ = request as? GoogleCheckRequest {
            if let userInfo = data?["data"] as? [String:Any] {
                let appInfo = AppInfo.default
                appInfo.install(data: userInfo)
                self.response?(ResponseState.mine_login,true, nil)
            }
        }
    }
    
    func requestDidSucess(_ request: BaseRequest) {
        
    }
    
    func requestDidFail(_ request: BaseRequest) {
        if let _ = request as? LoginRequest {
            self.response?(ResponseState.mine_login,false, request.errorMsg)
        }else if let _ = request as? GoogleCheckRequest {
            self.response?(ResponseState.mine_login,false, request.errorMsg)
        }
    }
    
    func requestParams(_ request: BaseRequest) -> [String : Any]? {
        var pramas: [String:Any] = [:]
        if let _ = request as? LoginRequest {
            pramas.updateValue(account, forKey: "loginName")
            pramas.updateValue(password, forKey: "loginPwd")
        }else if let _ = request as? GoogleCheckRequest {
            pramas.updateValue(self.googleCode, forKey: "googleCode")
            pramas.updateValue(self.googleToken, forKey: "googleToken")
            pramas.updateValue(self.loginName.count != 0 ? self.loginName : account, forKey: "loginName")
        }
        return pramas
    }
    
}

extension LoginViewModel: GoolPassWordDelegate {
    func popGoogleInputView() {
        GooglePassWordManager.creatGoolPassWord(dataSource: self, style: .googlePassWordStyle,validate: .googleStyle)
    }
    
    func googleVerificationt(passWord:String) {
        if passWord.count == 6 {
            self.googleCode = passWord
            self.googleApi.loadData()
        }
    }
    
}

